﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Threading;
using Practice2___DispatchHelper.Common;
using Practice2___DispatchHelper.Model;

namespace Practice2___DispatchHelper.ViewModel
{
    public class DispatcherHelperViewModel : ViewModelBase
    {
        public DispatcherHelperViewModel()
        {
            InitData();
            DispatcherHelper.Initialize();
        }

        private void InitData()
        {
            //UserList = new List<UserParam>()
            //{
            //    new UserParam(){ UserName="周杰伦", UserAddr="周杰伦地址", UserPhone ="88888888", UserSex="男" },
            //    new UserParam(){ UserName="刘德华", UserAddr="刘德华地址", UserPhone ="88888888", UserSex="男" },
            //    new UserParam(){ UserName="刘若英", UserAddr="刘若英地址", UserPhone ="88888888", UserSex="女" }
            //};
            UserList = new ObservableCollection<UserParam>()
            {
                new UserParam(){ UserName="周杰伦", UserAddr="周杰伦地址", UserPhone ="88888888", UserSex="男" },
                new UserParam(){ UserName="刘德华", UserAddr="刘德华地址", UserPhone ="88888888", UserSex="男" },
                new UserParam(){ UserName="刘若英", UserAddr="刘若英地址", UserPhone ="88888888", UserSex="女" }
            };
            inputUser = new UserParam();
        }

        private UserParam inputUser;
        /// <summary>
        /// 待添加的User
        /// </summary>
        public UserParam InputUser
        {
            get { return inputUser; }
            set
            {
                inputUser = value;
                RaisePropertyChanged(nameof(InputUser));
            }
        }

        //private List<UserParam> userList;
        ///// <summary>
        ///// 数据列表
        ///// </summary>
        //public List<UserParam> UserList
        //{
        //    get { return userList; }
        //    set
        //    {
        //        userList = value;
        //        RaisePropertyChanged(nameof(UserList));
        //    }
        //}
        private ObservableCollection<UserParam> userList;
        /// <summary>
        /// 数据列表
        /// </summary>
        public ObservableCollection<UserParam> UserList
        {
            get { return userList; }
            set
            {
                userList = value;
                RaisePropertyChanged(nameof(UserList));
            }
        }

        private int progressRate;
        /// <summary>
        /// 完成进度
        /// </summary>
        public int ProgressRate
        {
            get { return progressRate; }
            set
            {
                progressRate = value;
                RaisePropertyChanged(nameof(ProgressRate));
            }
        }

        private RelayCommand addUserCommand;
        /// <summary>
        /// 添加用户Command
        /// </summary>
        public RelayCommand AddUserCommand
        {
            get
            {
                if (addUserCommand == null)
                {
                    addUserCommand = new RelayCommand(() => ExecuteAddUser());
                }
                return addUserCommand;
            }
            set { addUserCommand = value; }
        }
        /// <summary>
        /// 执行添加用户Command
        /// </summary>
        private void ExecuteAddUser()
        {
            UserParam user = new UserParam();
            user.UserName = InputUser.UserName;
            user.UserPhone = InputUser.UserPhone;
            user.UserAddr = InputUser.UserAddr;
            user.UserSex = InputUser.UserSex;
            CreateUserInfoHelper ch = new CreateUserInfoHelper(user);
            ch.CreateProgress += ChOnCreateProgress;
            ch.CreateThread();
        }

        private void ChOnCreateProgress(object sender, CreateUserInfoHelper.CreateArgs e)
        {
            // Exception: System.InvalidOperationException: 某个 ItemsControl 与它的项源不一致
            //if (e.IsFinish)
            //{
            //    if (e.UserParam != null)
            //    {
            //        UserList.Add(e.UserParam);
            //    }
            //}
            //else
            //{
            //    ProgressRate = e.Progress;
            //}

            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                if (e.IsFinish)
                {
                    if (e.UserParam != null)
                    {
                        UserList.Add(e.UserParam);
                    }
                }
                else
                {
                    ProgressRate = e.Progress;
                }
            });
        }
    }
}
